/*
 * Copyright (c) 2022 Hong Jiahua
 * https://gitee.com/arrco/jh_queue
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 /**
 * @file        jh_queue.c
 * @brief       队列
 * @details     队列
 * @author      Hong Jiahua
 * @version     1.0.1
 * @date        2022-02-08
 * @copyright   2022 Hong Jiahua MIT License
 */
#include "jh_queue.h"

/************************************************************************/
/*                                                                      */
/************************************************************************/
/**
  * @brief      判断队列是否为空
  * @param[in]  queue   队列
  *
  * @return     errcode
  * @retval      1      队列为空
  * @retval      0      队列不为空
  * @retval     -1      失败
  */
int jh_queue_is_empty(jh_queue_t* queue) {
    if(queue == NULL)
        return -1;
    
    return queue->head == queue->tail;
}

/**
  * @brief      判断队列是否已满
  * @param[in]  queue   队列
  *
  * @return     errcode
  * @retval      1      队列已满
  * @retval      0      队列未满
  * @retval     -1      失败
  */
int jh_queue_is_full(jh_queue_t* queue) {
    if(queue == NULL)
        return -1;
    
    return (queue->tail + 1) % queue->num == queue->head;
}

/**
  * @brief      获取队列的数据数量
  * @param[in]  queue   队列
  *
  * @return     errcode
  * @retval     >=0     队列的数据数量
  */
size_t jh_queue_count(jh_queue_t* queue) {
    if(queue == NULL)
        return 0;
    
    return (queue->tail + queue->num - queue->head) % queue->num;
}

/**
  * @brief      数据入队
  * @param[in]  queue   队列
  * @param[in]  item    要入队的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_queue_push(jh_queue_t* queue, void* item) {
    if(queue == NULL || item == NULL || ((queue->tail + 1) % queue->num == queue->head))
        return -1;
    
    memcpy(queue->base + queue->tail * queue->size, item, queue->size);
    queue->tail = (queue->tail + 1) % queue->num;
    
    return 0;
}

/**
  * @brief      数据出队
  * @param[in]  queue   队列
  * @param[in]  item    要出队的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_queue_pop(jh_queue_t* queue, void* item) {
    if(queue == NULL || item == NULL || queue->head == queue->tail)
        return -1;
    
    memcpy(item, queue->base + queue->head * queue->size, queue->size);
    queue->head = (queue->head + 1) % queue->num;
    
    return 0;
}

/**
  * @brief      队尾数据出队
  * @param[in]  queue   队列
  * @param[in]  item    要出队的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_queue_pop_tail(jh_queue_t* queue, void* item) {
    if(queue == NULL || item == NULL || queue->head == queue->tail)
        return -1;
    
    queue->tail = queue->tail ? queue->tail - 1 : queue->num - 1;
    memcpy(item, queue->base + queue->tail * queue->size, queue->size);
    
    return 0;
}

/**
  * @brief      查看队首的数据
  * @param[in]  queue   队列
  * @param[in]  item    队首的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_queue_peek(jh_queue_t* queue, void* item) {
    if(queue == NULL || item == NULL || queue->head == queue->tail)
        return -1;
    
    memcpy(item, queue->base + queue->head * queue->size, queue->size);
    
    return 0;
}

/**
  * @brief      查看队首的数据
  * @param[in]  queue   队列
  * @param[in]  item    队首的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_queue_peek_head(jh_queue_t* queue, void* item) {
    return jh_queue_peek(queue, item);
}

/**
  * @brief      查看队尾的数据
  * @param[in]  queue   队列
  * @param[in]  item    队尾的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_queue_peek_tail(jh_queue_t* queue, void* item) {
    if(queue == NULL || item == NULL || queue->head == queue->tail)
        return -1;
    
    memcpy(item, queue->base + (queue->tail ? (queue->tail - 1) : (queue->num - 1)) * queue->size, queue->size);
    
    return 0;
}

/**
  * @brief      清空队列
  * @param[in]  queue   队列
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_queue_clear(jh_queue_t* queue) {
    if(queue == NULL)
        return -1;
    
    return queue->head = queue->tail = 0;
}

/**
  * @brief      获取队列指定位置的指定数量数据
  * @param[in]  queue   队列
  * @param[in]  item    获取的数据项
  * @param[in]  start   指定的起始位置
  * @param[in]  num     指定的数量
  *
  * @return     errcode
  * @retval     >=0     成功获取的数据数量
  * @retval     -1      失败
  */
int jh_queue_get_data(jh_queue_t* queue, void* item, size_t start, size_t num) {
    if(queue == NULL || item == NULL)
        return -1;
    
    size_t count = (queue->tail + queue->num - queue->head) % queue->num;
    if(start >= count)
        return 0;
    
    size_t vaild_len = (count - start) < num ? (count - start) : num;
    size_t all_tail_len = queue->tail > queue->head ? queue->tail - queue->head : queue->num - queue->head;
    size_t tail_len = all_tail_len > start ? ((all_tail_len - start) < vaild_len ? all_tail_len - start : vaild_len) : 0;
    memcpy(item, queue->base + (all_tail_len > start ? queue->head + start : 0) * queue->size, tail_len * queue->size);
    memcpy(item + tail_len * queue->size, queue->base + (all_tail_len > start ? 0 : start - all_tail_len) * queue->size, (vaild_len - tail_len) * queue->size);
    
    return vaild_len;
}

/**
  * @brief      队列初始化
  * @param[in]  queue   队列
  * @param[in]  base    指向用于队列的数组
  * @param[in]  num     数组中能存放的数据项数量
  * @param[in]  size    数组中每个数据项的大小
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_queue_init(jh_queue_t* queue, void* base, size_t num, size_t size) {
    if(queue == NULL || base == NULL)
        return -1;
    
    queue->base = base;
    queue->num = num;
    queue->size = size;
    queue->head = 0;
    queue->tail = 0;
    
    return 0;
}
